package com.easylinkin.linkappapi.grid.controller;

import cn.hutool.core.date.DateUtil;
import com.easylinkin.core.utils.HttpFileUtils;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.grid.entity.GridAppraise;
import com.easylinkin.linkappapi.grid.models.dto.GridAppraiseDTO;
import com.easylinkin.linkappapi.grid.models.dto.GridAppraiseFlagDTO;
import com.easylinkin.linkappapi.grid.models.query.GridAppraiseQuery;
import com.easylinkin.linkappapi.grid.service.IGridAppraiseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;


/**
 * 网格评比表 前端控制器
 *
 * @author
 * @since 2024-05-16
 */
@Api(tags = "网格评比表")
@RestController
@RequestMapping("/grid/gridAppraise")
public class GridAppraiseController {
    @Autowired
    private IGridAppraiseService gridAppraiseService;

    /**
     * 分页列表查询
     *
     * @param requestModel
     * @return
     */
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PostMapping(value = "/listPage")
    public RestMessage queryPageList(@RequestBody RequestModel<GridAppraiseQuery> requestModel) {
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        return this.gridAppraiseService.queryPageList(requestModel);
    }

    /**
     * 导出
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "导出", notes = "导出")
    @PostMapping(value = "/export")
    public ResponseEntity<byte[]> export(@RequestBody GridAppraiseQuery query) {
        byte[] bytes = gridAppraiseService.export(query);
        return HttpFileUtils.exportFile(bytes, String.format("网格评比导出(%s).zip", DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss")));
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询", httpMethod = "GET")
    @GetMapping(value = "/queryInfo/{id}")
    public RestMessage queryInfo(@PathVariable("id")Integer id) {
        GridAppraise gridAppraise = this.gridAppraiseService.getById(id);
        return RestBuilders.successBuilder().data(gridAppraise).build();
    }

    /**
     * 保存
     *
     * @param gridAppraiseDTO
     * @return
     */
    @ApiOperation(value = "保存", notes = "保存")
    @PostMapping(value = "/save")
    public RestMessage save(@Validated @RequestBody GridAppraiseDTO gridAppraiseDTO) {
        return gridAppraiseService.addAppraise(gridAppraiseDTO);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/delete")
    public RestMessage delete(@RequestParam("id") Integer id) {
        gridAppraiseService.removeById(id);
        return RestBuilders.successBuilder().message("删除成功").build();
    }

    @ApiOperation(value = "设置红/黄旗单位", notes = "设置红/黄旗单位")
    @PostMapping(value = "/setAppraiseFlag")
    public RestMessage setAppraiseFlag(@Validated @RequestBody GridAppraiseFlagDTO gridAppraiseFlagDTO) {
        return gridAppraiseService.setAppraiseFlag(gridAppraiseFlagDTO);
    }

}
